//ajax
$("#tbl_user").bootstrapTable({
    url:"chushou/all",
    method:'post',
    toolbar:'#toolbar',
    contentType:'application/x-www-form-urlencoded',
    sidePagination:'server',
    pageNumber:1,
    pageSize:5,
    queryParams:function(params){
        params["cs_name"] = $("#username").val();
        params["cs_id"] = $("#dingdan").val();
        return params;
    },
    pageList:[5, 25, 50, 100,'ALL'],
    dataType:'json',
    cache:false,
    pagination:true,
    detailView:true,
    detailFormatter:function (index,row,value) {
        return "<div><table id='table_dizhi"+index+"'></table></div>";
    },


    showRefresh:true,
    showColumns:true,
    classes:'table table-hover table-striped',
    columns:[
        {
            checkbox:true
        },{
            title:"序号",
            align:"center",
            formatter:function(value,row,index){
                return (index + 1);
            }
        },
        {
            field:"cs_id",
            title:"订单号",
            align:"center"
        },{
            field:"cs_name",
            title:"买家姓名",
            align:"center"
        },{
            field:"cs_shouji",
            title:"联系方式",
            align:"center"
        },{
            field:"cs_shenfen",
            title:"身份证号",
            align:"center"
        },{
            field:"cs_dizhi",
            title:"家庭住址",
            align:"center"
        },{
            field:"cs_jine",
            title:"订单总金额",
            align:"center"
        },{
            field:"cs_riqi",
            title:"下单时间",
            align:"center"
        },{
            title:"操作",
            align:"center",
            formatter:function (value, row, index) {
                var html = [
                    "<a href='javascript:void(0);' onclick='delRow(\""+row.cs_id+"\")'><span class='glyphicon glyphicon-remove'></span></a>",
                ];
                return html.join("");
            }
        }
    ],onExpandRow:function(index, row, $Subdetail){
        InitSubTable(index, row, $Subdetail);
    }

});

//删除一行数据
function delRow(row) {
    swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "我再想想吧!",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "删除",
        closeOnConfirm: false
    }, function () {
        $.ajax({
            url:'chushou/delete',
            method:"post",
            data:{"id":row},
            success:function (data) {
                swal("删除成功！", "您已经永久删除了这条信息。", "success");
                //刷新表格
                $("#tbl_user").bootstrapTable('refresh');
            }
        });


    });
}

InitSubTable = function (index, row) {
    var parentid = row.cs_id;
    $("#table_dizhi"+index).bootstrapTable({
        url: 'chushou/getById',
        method: 'post',
        contentType:'application/x-www-form-urlencoded',
        sidePagination:'server',
        dataType:'json',
        queryParams: { id: parentid },
        ajaxOptions: { id: parentid },
        clickToSelect: true,
        pageSize: 10,
        pageList: [10, 25],
        columns: [{
                title:"序号",
                align:"center",
                formatter:function(value,row,index){
                    if(row.dd_tuihui != 0){
                        return "<span style='color: red'>已退回</span>";
                    }
                    return (index + 1);
                }
            },{
            field:"ts_id.tu_name",
            title:"图书名称",
            align:"center"
        },{
            field:"ts_id.jieyue",
            title:"借阅价格",
            align:"center"
        },{
            field:"ts_id.chushou",
            title:"出售价格",
            align:"center"
        },{
            field:"ts_id.posun",
            title:"破损量比",
            align:"center"
        },{
            field:"ts_id.cishu",
            title:"借阅次数",
            align:"center"
        },{
            field:"ts_id.lx.lx_name",
            title:"类型",
            align:"center"
        },{
            field:"ts_id.chubanshe.cbs_name",
            title:"出版社名字",
            align:"center"
        },{
            field:"ts_id.book_img",
            title:"图书图片",
            align:"center",
            formatter:function (value, row, index) {
                return '<img src="'+row.ts_id.book_img+'" width="80px" />';
            }
        }
         ]
    });
};


function chang(i){
    var id = $(i).val();
    var index = parseInt($(i).parents(".tushu").index()) - 4;
    var zongjia = 0.0;
    var fou = 0;
    // var geshu = $(i).parents("#myForm").children(".tushu:last").index();
    for(var k = 0;k<index;k++){
        var geshu = parseInt($(i).parents("#myForm").children(".tushu:eq("+k+")").find(".ts_id").val());
        if(id == geshu){
            fou = 1;
        }
    }
    if(id != "" && fou != 1){
        $.ajax({
            url:'chushou/getTSId',
            method:"post",
            data:{"id":id},
            dataType : 'json',
            success:function (data) {
                   if(data.rows.zt_id != 2){
                       if(data.rows != null){
                           $(i).parents(".tushu").children(".form-group").show();
                           $(i).parents(".tushu").find(".ts_name").val(data.rows.tu_name)
                           $(i).parents(".tushu").find(".ts_jine").val(data.rows.chushou)
                           for(var k = 0;k<4;k++){
                               if($(".tushu:eq("+k+")").find(".ts_jine").val() != null && $(".tushu:eq("+k+")").find(".ts_jine").val() != undefined && $(".tushu:eq("+k+")").find(".ts_jine").val() != ""){
                                   zongjia += parseFloat($(".tushu:eq("+k+")").find(".ts_jine").val());

                                   $("#cs_jine").val(zongjia.toFixed(2))
                               }
                           }
                           $(i).parents(".tushu").next().show();
                       } else{
                           $(i).parents(".tushu").find(".ts_name").val("")
                           $(i).parents(".tushu").find(".ts_jine").val("")
                           $(i).parents(".tushu").children(".form-group:eq(0)").siblings().hide();
                           $(i).parents(".tushu").next().hide();
                           for(var k = 0;k<4;k++){
                               if($(".tushu:eq("+k+")").find(".ts_jine").val() != null && $(".tushu:eq("+k+")").find(".ts_jine").val() != undefined && $(".tushu:eq("+k+")").find(".ts_jine").val() != ""){
                                   zongjia += parseFloat($(".tushu:eq("+k+")").find(".ts_jine").val());
                                   $("#cs_jine").val(zongjia.toFixed(2))
                               }
                           }
                       }
                   }
               }


        });
    }else{
        $(i).parents(".tushu").find(".ts_name").val("")
        $(i).parents(".tushu").find(".ts_jine").val("")
        $(i).parents(".tushu").children(".form-group:eq(0)").siblings().hide();
        $(i).parents(".tushu").next().hide();
        for(var k = 0;k<4;k++){
            if($(".tushu:eq("+k+")").find(".ts_jine").val() != null && $(".tushu:eq("+k+")").find(".ts_jine").val() != undefined && $(".tushu:eq("+k+")").find(".ts_jine").val() != ""){
                zongjia += parseFloat($(".tushu:eq("+k+")").find(".ts_jine").val());
            }

            $("#cs_jine").val(zongjia.toFixed(2))
        }
    }
}



//显示模态框并打开新增界面
function toAdd(title,url) {
    $("#modal1").find(".modal-title").html(title);
    $("#modal1").find(".modal-body").load(url);
    $("#modal1").modal('show');
}

//提交表单
function submitForm(btn) {
    $("#cs_jine").removeAttr("disabled");
    var $form = $(btn).parents("#modal1").find("#myForm");
    $($form).find("#dz_shenFen").removeAttr("disabled");
    var action = $form.attr("action");
    var str = $form.serialize();
    $.ajax({
        url:action,
        method:"post",
        //data:str,
        //FormData(dom)
        data:str,
        dataType:'json',
        // processData: false,
        // contentType: false,
        success:function (data) {
            if(data.result){
                swal("成功", "操作成功", "success");
                //隐藏模态框
                $("#modal1").modal('hide');
                //刷新表格
                $("#tbl_user").bootstrapTable('refresh');

            }
        }
    });
}

//按条件查询
function setCondition() {
    $("#tbl_user").bootstrapTable('refresh');
}

//批量删除
function batchDel() {
    //1.判断是否有行被选中
    //getSelections
    //如果没有行被选中,返回[]
    var rows = $("#tbl_user").bootstrapTable("getSelections");
    if(rows.length == 0){
        swal("提示", "请选中要删除的行", "warning");
        return;
    }
    //2.1获取要删除的行(主键)  []
    //2.1获取要删除的行(主键)  []
    var model = {};
    for(var i = 0;i<rows.length;i++){
        model["ids["+i+"]"] = rows[i].cs_id
    }
//2.2传递数据到Controller
    swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "我再想想吧!",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "删除",
        closeOnConfirm: false
    }, function () {
        //ajax
        $.ajax({
            url:"chushou/batchDel",
            method:"post",
            data:model,
            dataType:'json',
            success:function (data) {
                if(data.result){
                    swal("添加成功！", "您已经永久添加了这些信息。", "success");
                    //隐藏模态框
                    $("#modal1").modal('hide');
                    //刷新表格
                    $("#tbl_user").bootstrapTable('refresh');

                }
            }
        });

    });
}

function batchAdd(title,url) {
    $("#modalExcel").find(".modal-title").html(title);
    $("#modalExcel").find(".modal-body").load(url);
    $("#modalExcel").modal('show');
}

function batchInsert() {
    var $form = $("#modalExcel").find('#myFormXls');
    var action = $form.attr('action');
    $.ajax({
        url:action,
        method:"post",
        data:new FormData($form.get(0)),
        dataType:'json',
        processData: false,
        contentType: false,
        success:function (data) {
            if(data.result){
                swal("添加成功！", "您已经永久添加了这些信息。", "success");
                //隐藏模态框
                $("#modalExcel").modal('hide');
                //刷新表格
                $("#tbl_user").bootstrapTable('refresh');

            }
        }
    });
}

function exportExcel(url) {
    var form = document.createElement('form');
    form.setAttribute("action",url);
    form.setAttribute("method","post");
    document.getElementsByTagName('body')[0].appendChild(form);
    form.submit();
}